/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author RYUDO
 */
public class Database_function {
    /** initialize the Connection **/
    public static Connection setConnection() {
        Connection cn = null;
        File f = new File("DB.con");
        FileInputStream fi = null;
        try {
            fi = new FileInputStream(f);
            Properties prop = new Properties();
            prop.load(fi);
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            cn = DriverManager.getConnection("jdbc:sqlserver://" +prop.getProperty("server") + "\\MSSQLSERVER:" + prop.getProperty("DBPort") + ";database=" +prop.getProperty("DBName") + ";username=" + prop.getProperty("DBUsername") + ";password=" + prop.getProperty("DBPassword") + "");
            return cn;
            
        } catch (IOException ex) {
            Logger.getLogger(Database_function.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            try {
                JOptionPane.showMessageDialog(null, "Your database config file is not correct! Please restart or re-install the application");
                fi.close();
                f.delete();
                System.exit(0);
            } catch (IOException ex1) {
                Logger.getLogger(Database_function.class.getName()).log(Level.SEVERE, null, ex1);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Database_function.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fi.close();
                
            } catch (IOException ex) {
                Logger.getLogger(Database_function.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
    /** Get the user role **/
    public static void close_connection(Object input) {
        try {
            if(input instanceof Connection) {
                if(!((Connection)input).isClosed()) {
                    ((Connection)input).close();
                }
            } else
             if(input instanceof ResultSet) {
                if(!((ResultSet)input).isClosed()) {
                    ((ResultSet)input).close();
                }
            } else
            if(input instanceof PreparedStatement) {
                if(!((PreparedStatement)input).isClosed()) {
                    ((PreparedStatement)input).close();
                }
             }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Database Encountered Critical Error ! Exit the program ! Contact your administrator");
        }
    }

    
}
